package com.example.medicalequipment.ui.fragment;

import android.os.Bundle;


import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.medicalequipment.R;
import com.example.medicalequipment.controller.RepairController;
import com.example.medicalequipment.entity.MedicalDevice;

import java.util.Date;

public class RepairFragment extends Fragment implements RepairController.OnRepairSubmitListener {
    private EditText etDeviceName, etDeviceCode, etFaultDescription, etReporter;
    private Spinner spnDepartment;
    private Button btnSubmit;
    private RepairController controller;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_repair, container, false);
        initViews(view);
        initController();
        setupDepartmentSpinner();
        setupSubmitButton();
        return view;
    }

    private void initViews(View view) {
        etDeviceName = view.findViewById(R.id.et_device_name);
        etDeviceCode = view.findViewById(R.id.et_device_code);
        etFaultDescription = view.findViewById(R.id.et_fault_description);
        etReporter = view.findViewById(R.id.et_reporter);
        spnDepartment = view.findViewById(R.id.spn_department);
        btnSubmit = view.findViewById(R.id.btn_submit);
    }

    private void initController() {
        controller = new RepairController(requireContext(), this);
    }

    private void setupDepartmentSpinner() {
        // 设置科室下拉列表
        String[] departments = getResources().getStringArray(R.array.departments);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                requireContext(), android.R.layout.simple_spinner_item, departments);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnDepartment.setAdapter(adapter);
    }

    private void setupSubmitButton() {
        btnSubmit.setOnClickListener(v -> validateAndSubmit());
    }

    private void validateAndSubmit() {
        String deviceName = etDeviceName.getText().toString().trim();
        String deviceCode = etDeviceCode.getText().toString().trim();
        String department = spnDepartment.getSelectedItem().toString();
        String faultDescription = etFaultDescription.getText().toString().trim();
        String reporter = etReporter.getText().toString().trim();

        if (TextUtils.isEmpty(deviceName)) {
            etDeviceName.setError("请输入设备名称");
            etDeviceName.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(deviceCode)) {
            etDeviceCode.setError("请输入设备编码");
            etDeviceCode.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(faultDescription)) {
            etFaultDescription.setError("请描述设备故障情况");
            etFaultDescription.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(reporter)) {
            etReporter.setError("请输入报修人姓名");
            etReporter.requestFocus();
            return;
        }

        // 创建医疗设备对象（使用默认图片资源）
        MedicalDevice device = new MedicalDevice(
                deviceName,
                department,
                R.drawable.devices_1, // 默认设备图片
                deviceCode,
                "", // 功能描述（可选）
                "", // 设备介绍（可选）
                faultDescription,
                reporter,
                new Date().getTime(),
                0 // 状态：待处理
        );

        // 提交维修请求
        controller.submitRepairRequest(device);
    }

    @Override
    public void onRepairSubmitted(boolean success, String message) {
        Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
        if (success) {
            clearFormFields();
        }
    }

    private void clearFormFields() {
        etDeviceName.setText("");
        etDeviceCode.setText("");
        etFaultDescription.setText("");
        etReporter.setText("");
        spnDepartment.setSelection(0);
    }
}